/**
 * 判断对象是否存在
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 存在返回true,否则返回false
 */
zxt.util.isAvailable = function(obj) {
	if (obj === undefined) {
		return false;
	}
	;
	if (obj === false) {
		return true;
	}
	;
	if (!obj) {
		return false;
	}
	;
	if (obj == "") {
		return false;
	}
	;
	return true;
};

/**
 * 判断对象是否是一个function
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 是一个function返回true,否则返回false
 */
zxt.util.isFunction = function(obj) {
	return (typeof (obj) == "function");
};

/**
 * 判断对象是否是一个数组
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 是一个数组返回true,否则返回false
 */
zxt.util.isArray = function(obj) {
	return (typeof obj == "array" || obj instanceof Array);
};

/**
 * 判断对象是否是一个string
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 是一个string返回true,否则返回false
 */
zxt.util.isString = function(obj) {
	return (typeof (obj) == "string");
};

/**
 * 判断对象是否是一个不为空的string
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 是一个不为空的string返回true,否则返回false
 */
zxt.util.isLengthString = function(obj) {
	return ((typeof (obj) == "string") && (obj !== ""));
};

/**
 * 判断对象是否是一个为空的string
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 是一个为空的string返回true,否则返回false
 */
zxt.util.isEmptyString = function(obj) {
	return ((typeof (obj) == "string") && (obj === ""));
};

/**
 * 把对象格式化成字符串
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {String} 格式化的字符串
 */
zxt.util.toString = function(obj) {
	return JSON2String(obj);
};

/**
 * 判断对象是否是一个数字
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} true - 指定对象是数字, false - 其他情况
 */
zxt.util.isNumber = function(obj) {
	return (typeof obj == "number");
};

/**
 * 判断对象是否是undefined
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 是undefined返回true,否则返回false
 */
zxt.util.isUndefined = function(obj) {
	return (obj === undefined);
};

/**
 * 判断对象是否为null
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 是null返回true,否则返回false
 */
zxt.util.isNull = function(obj) {
	return (obj === null);
};

/**
 * 判断对象是否不为null
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 不为null返回true,否则返回false
 */
zxt.util.isNotNull = function(obj) {
	return (obj !== null);
};

/**
 * 判断对象是否有值
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 有值返回true,否则返回false
 */
zxt.util.isValue = function(obj) {
	return ((obj !== undefined) && (obj !== null));
};

/**
 * 判断对象是NULL或undefined
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 无值返回true,否则返回false
 */
zxt.util.notValue = function(obj) {
	return ((obj === undefined) || (obj === null));
};

/**
 * 消息框提示对象中的每一个元素及其值
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 */
zxt.util.alert = function(obj) {
	for ( var k in obj) {
		alert(k + ": " + obj[k]);
	}
	;
};

/**
 * 判断map是否为空
 * 
 * @function
 * @static
 * @param {object}
 *            obj 任意对象
 * @return {boolean} 为空返回true,否则返回false
 */
zxt.util.isEmpty = function(map) {
	for ( var k in map) {
		return false;
	}
	;
	return true;
};

/**
 * 返回字符串长度
 * 
 * @function
 * @static
 * @param {string}
 *            str 字符串对象
 * @return {int} 字符串长度
 */
zxt.util.getByteLength = function(str) {
	if (str == null || str == "")
		return 0;
	var totalCount = 0;
	for (i = 0; i < str.length; i++) {
		if (str.charCodeAt(i) > 127)
			totalCount += 2;
		else
			totalCount++;
	}
	return totalCount;
};

/**
 * 返回唯一标识
 * 
 * @function
 * @static
 * @return {string} 唯一标识
 */
zxt.util.GUID = function() {
	var guid = "";
	for ( var i = 1; i <= 32; i++) {
		var n = Math.floor(Math.random() * 16.0).toString(16);
		guid += n;
		if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
			guid += "-";
	}
	return guid;
};

zxt.util.formatDecimal = function(p) {
	var digits = -1;
	var num, ceil;
	do {
		digits = digits + 1;
		var power = Math.pow(10, digits);
		num = p * power;
		ceil = Math.ceil(num);
	} while (ceil > num);

	var ss = p.toFixed(digits);
	return ss;
};

zxt.util.formatInteger = function(p) {
	var ret = "";
	var num = p;
	do {
		var s = num % 10;
		var f = (num - s) / 10;
		ret = s.toString() + ret;
		num = f;
	} while (num > 0);

	return ret;
};

/**
 * 返回url中的参数值
 * 
 * @function
 * @static
 * @param {string}
 *            href 链接字符串
 * @param {string}
 *            name 参数名
 * @return {string} url中的参数值
 */
zxt.util.getQueryInURL = function(href, name) {
	return zxt.util._getQueryInURL1(href, name);
};

/* dom */
zxt.util.dom = {

	getBorderLeft : function(el) {
		if (system.isIE) {
			return el.clientLeft
		}
		;
		return parseInt(window.getComputedStyle(el, null).getPropertyValue(
				"border-left-width"));
	},

	getInnerLeft : function(el) {
		if (el == null)
			return 0;
		if (system.isIE && el == document.body || system.isFF
				&& el == document.documentElement)
			return 0;
		return zxt.util.dom.getLeft(el) + zxt.util.dom.getBorderLeft(el);
	},

	getLeft : function(el) {
		if (el == null)
			return 0;
		return el.offsetLeft + zxt.util.dom.getInnerLeft(el.offsetParent);
	},

	getBorderTop : function(el) {
		if (system.isIE) {
			return el.clientTop
		}
		;
		return parseInt(window.getComputedStyle(el, null).getPropertyValue(
				"border-top-width"));
	},

	getInnerTop : function(el) {
		if (el == null)
			return 0;
		if (system.isIE && el == document.body || system.isFF
				&& el == document.documentElement)
			return 0;
		return zxt.util.dom.getTop(el) + zxt.util.dom.getBorderTop(el);
	},

	getTop : function(el) {
		if (el == null)
			return 0;
		var ptScroll = 0;
		if (el.offsetParent != null) {
			ptScroll = el.offsetParent.scrollTop;
		}
		;
		return el.offsetTop + zxt.util.dom.getInnerTop(el.offsetParent)
				- ptScroll;

	},

	/* Rectangle Getters */
	getOuterRect : function(el) {
		return {
			left : zxt.util.dom.getLeft(el),
			top : zxt.util.dom.getTop(el),
			width : el.offsetWidth,
			height : el.offsetHeight
		};
	},

	getDocumentRect : function() {
		var _width = 0;
		var _height = 0;
		if (system.isIE) {
			_width = document.body.clientWidth;
			_height = (document.documentElement.clientHeight == 0) ? document.body.clientHeight
					: document.documentElement.clientHeight;
		} else {
			_width = window.innerWidth;
			_height = window.innerHeight;
		}
		;

		return {
			left : 0,
			top : 0,
			width : _width,
			height : _height
		};
	},

	getScrollPos : function(el) {
		var _left = 0;
		var _top = 0;
		if (system.isIE) {
			_left = document.body.scrollLeft;
			_top = document.body.scrollTop;
		} else {
			_left = window.pageXOffset;
			_top = window.pageYOffset;
		}
		;

		return {
			left : _left,
			top : _top
		};
	},

	_border : {
		borderLeft : 1,
		borderRight : 1,
		borderTop : 1,
		borderBottom : 1
	},

	_padding : {
		paddingLeft : 1,
		paddingRight : 1,
		paddingTop : 1,
		paddingBottom : 1
	},

	_shaddow : {
		shadowLeft : 0,
		shadowRight : 0,
		shadowTop : 0,
		shadowBottom : 0
	},

	postionWithRel : function(el, relEl, hor, sBorder, sPadding, sShaddow) {
		var border = (sBorder == undefined) ? zxt.util.dom._border : sBorder;
		var padding = (sPadding == undefined) ? zxt.util.dom._padding
				: sPadding;
		var shaddow = (sShaddow == undefined) ? zxt.util.dom._shaddow
				: sShaddow;
		var _left = 0;
		var _top = 0;

		var piRect = relEl;
		if (relEl.left == null || relEl.top == null || relEl.width == null
				|| relEl.height == null) {
			piRect = zxt.util.dom.getOuterRect(relEl);
		}

		var menuRect = zxt.util.dom.getOuterRect(el);
		var docRect = zxt.util.dom.getDocumentRect();
		var scrollPos = zxt.util.dom.getScrollPos();

		if (!hor) {
				_left = $(relEl).offset().left;

			if (piRect.top + piRect.height + menuRect.height <= docRect.height
					+ scrollPos.top)
				_top = $(relEl).offset().top + piRect.height;
			else if (piRect.top - menuRect.height >= scrollPos.top)
				_top = $(relEl).offset().top - menuRect.height;
			else if (docRect.height >= menuRect.height)
				_top = docRect.height + scrollPos.top - menuRect.height;
			else
				_top = scrollPos.top;
		} else {
			if (piRect.top + menuRect.height - border.borderTop
					- padding.paddingTop <= docRect.height + scrollPos.top)
				_top = piRect.top - border.borderTop - padding.paddingTop;
			else if (piRect.top + piRect.height - menuRect.height
					+ border.borderTop + padding.paddingTop >= 0)
				_top = piRect.top + piRect.height - menuRect.height
						+ border.borderBottom + padding.paddingBottom
						+ shaddow.shadowBottom;
			else if (docRect.height >= menuRect.height)
				_top = docRect.height + scrollPos.top - menuRect.height;
			else
				_top = scrollPos.top;

			if (piRect.left + piRect.width + menuRect.width
					+ padding.paddingRight + border.borderRight
					- border.borderLeft + shaddow.shadowRight <= docRect.width
					+ scrollPos.left)
				_left = piRect.left + piRect.width + padding.paddingRight
						+ border.borderRight - border.borderLeft;
			else if (piRect.left - menuRect.width - padding.paddingLeft
					- border.borderLeft + border.borderRight
					+ shaddow.shadowRight >= 0)
				_left = piRect.left - menuRect.width - padding.paddingLeft
						- border.borderLeft + border.borderRight
						+ shaddow.shadowRight;
			else if (docRect.width >= menuRect.width)
				_left = docRect.width + scrollPos.left - menuRect.width;
			else
				_left = scrollPos.left;
		}

		el.style.left = _left + "px";
		el.style.top = _top + "px";
		return {
			left : _left,
			top : _top
		};
	}

};

/**
 * 设置DOM节点是否enable
 * 
 * @function
 * @static
 * @param {DOM}
 *            element DOM节点
 * @param {boolean}
 *            enabled 是否enable
 */
zxt.util.enable = function(element, enabled) {
	if (element.nodeName != "DIV") {
		element.disabled = enabled == true ? false : true;
	} else {
		for ( var i = 0; i < element.children.length; i++) {
			zxt.util.enable(element.children[i], enabled);
		}
	}
	;
};

/**
 * 获得父窗口
 * 
 * @function
 * @static
 * @param {DOM}
 *            window 当前的window
 * @returns {DOM} 父窗口, 如果已经为顶级窗口, 返回null;
 */
zxt.util.getParentWindow = function(window) {
	if (window.parent == window)
		return null;
	return window.parent;
};

/**
 * 获得最上层的window
 * 
 * @function
 * @static
 * @param {DOM}
 *            window 当前的window
 * @returns {DOM} 最上层的window
 */
zxt.util.getTopWindow = function(window) {
	_window = window.parent;
	while (_window.parent != _window) {
		_window = _window.parent;
	}
	;
	return _window;
};

/**
 * 字符串分割成选项，以","来分割每一项，以":"来分割label和value
 * 
 * @function
 * @static
 * @param {string}
 *            window 当前的window
 * @returns {option[]} 选项数组
 * @returns {option.label} 选项的标签
 * @returns {option.value} 选项的值
 */
zxt.util.cutStr = function(str) {
	var strs = str.split(",");
	var rsStrs = new Array();
	for ( var i = 0; i < strs.length; i++) {
		var cutStr = strs[i].split(":");
		var option = {
			label : cutStr[0],
			value : cutStr[1]
		};
		rsStrs.push(option);
	}
	;
	return rsStrs;
};

zxt.util.getAppName = function() {
	var idx = 0;
	// fix modal window problem
	if (window.location.pathname.charAt(0) == '/') {
		idx = 1;
	}
	var splitter = "/";
	if (window.location.pathname.indexOf('\\') >= 0) {
		splitter = '\\';
	}
	var _name = window.location.pathname.substring(idx).split(splitter)[0];
	return _name ? _name == "" ? "POR" : _name : "POR";
};

zxt.util.getImagePath = function() {
	if (window.location.host) {
		return "/" + zxt.util.getAppName + "/zxt/images/";
	} else {
		return "../../Images/";
	}
};

zxt.util.parseLocation = function() {
	var queryString = location.search.substring(1, location.search.length);
	var querys = queryString.split("&");
	var queryMap = {};
	for ( var i = 0; i < querys.length; i++) {
		var q = querys[i].split("=");
		queryMap[q[0]] = q[1];
	}
	return queryMap;
};

zxt.util._getQueryInURL1 = function(href, str) {
	var rs = new RegExp("(^|)" + str + "=([^\&]*)(\&|$)", "gi").exec(href), tmp;
	if (tmp = rs) {
		return tmp[2];
	}
	return null;
};

zxt.util._getQueryInURL2 = function(href, name) {

};

zxt.util.stringXSSFilter = function(str) {
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	return str;
};

zxt.util.stringContains = function(target, sub) {
	return (target.indexOf(sub) >= 0);
};

zxt.util.stringStartWith = function(target, sub) {
	return (target.indexOf(sub) == 0);
};

zxt.util.stringPadLeft = function(str, len, trmL) {
	if (str.length >= len) {
		if (trmL === true) {
			return str.substr((str.length - len), len);
		} else if (trmL === false) {
			return str.substr(0, len);
		} else {
			return str;
		}
		;
	}
	;

	return zxt.util.stringPadLeft("0" + str, len);
};

zxt.util.stringPadRight = function(str, len, trmL) {

	if (str.length >= len) {
		if (trmL === true) {
			return str.substr((str.length - len), len);
		} else if (trmL === false) {
			return str.substr(0, len);
		} else {
			return str;
		}
		;
	}
	;
	return zxt.util.stringPadRight(str + "0", len);
};

zxt.util.stringNumberFormat = function(str, scale) {
	var parts = str.split(".");

	if (zxt.util.notValue(parts[1])) {
		parts[1] = zxt.util.stringPadRight('0', scale);
	} else {
		parts[1] = zxt.util.stringPadRight(parts[1], scale, false);
	}
	;

	return parts.join(".");
};

zxt.util.isDateString = function(dateStr) {
	var B, D = 0;
	// Split into $1 $2 $3
	B = /^(\d{4})-(\d|\d\d)-(\d|\d\d)$/.test(dateStr);
	if (B)
		with (RegExp) {
			// $1 $2 $3 = Y M D
			D = new Date($1, $2 - 1, $3);
			// test Y M D valid
			B = ((D.getMonth() == $2 - 1) && (D.getFullYear() == $1));
		}
	return B;
};

zxt.util.isDate = function(y, m, d) {
	var D = new Date(y, m - 1, d);
	var B = ((D.getMonth() == m - 1) && (D.getFullYear() == y));
	return B;
};

zxt.util.utilsFormat = function(value, type) {
	if (!zxt.util.isAvailable(type)) {
		return true;
	}
	
	var res = this.getRegs(type);
	if (value == "" || value.length == 0) {
		return true;
	}
	var transChineseVal = value.replace(/[^\x00-\xff]/g, "**");
	var reg = new RegExp(res);
	return reg.test(transChineseVal);
};

zxt.util.stringLength = function(str) {
	return str.replace(/[^\x00-\xff]/g, "**").length;
};

zxt.util.getRegs = function(key) {
	if (typeof (key) != "string") {
		return key;
	}
	var res = key;
	var str = key.substring(0, 1);
	if (str == "A" || str == "a") {
		var length = key.replace(/[^0-9]/ig, "");
		res = "^\.{0," + length + "}$";
	} else if (str == "N" || str == "n") {
		if (key.indexOf(",") > 0) {
			var ress = key.split(",");
			var left = ress[0].replace(/[^0-9]/ig, "");
			var right = ress[1].replace(/[^0-9]/ig, "");
			res = "^([0-9]{1," + (left - right) + "}(\\.[0-9]{1," + right
					+ "})?)?$";
		} else {
			var length = key.replace(/[^0-9]/ig, "");
			res = "^([0-9]{1," + length + "})?$";
		}
	} else if (str == "+" && key.length > 2
			&& (key.substring(1, 2) == "N" || key.substring(1, 2) == "n")) {
		if (key.indexOf(",") > 0) {
			var ress = key.split(",");
			var left = ress[0].replace(/[^0-9]/ig, "");
			var right = ress[1].replace(/[^0-9]/ig, "");
			res = "^([\+-]?[0-9]{1," + (left - right) + "}(\\.[0-9]{1," + right
					+ "})?)?$";
		} else {
			var length = key.replace(/[^0-9]/ig, "");
			res = "^([\+-]?[0-9]{1," + length + "})?$";
		}
	} else if (key == "CARD") {
		res = "^[0-9]{16}$";
	} else if (key == "CARD-15") {
		res = "^[0-9]{15}$";
	} else if (key == "CARD-19") {
		res = "^[0-9]{19}$";
	}
	return res;
};